home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kzip.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  10.6 KB  |  285 lines

  1. /* This file is part of the KDE libraries
  2.    Copyright (C) 2002 Holger Schroeder <holger-kde@holgis.net>
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License version 2 as published by the Free Software Foundation.
  7.  
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.  
  13.    You should have received a copy of the GNU Library General Public License
  14.    along with this library; see the file COPYING.LIB.  If not, write to
  15.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16.    Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef __kzip_h
  19. #define __kzip_h
  20.  
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23.  
  24. #include <qdatetime.h>
  25. #include <qstring.h>
  26. #include <qstringlist.h>
  27. #include <qdict.h>
  28. #include <qvaluelist.h>
  29. #include <karchive.h>
  30.  
  31. class KZipFileEntry;
  32. /**
  33.  *   This class implements a kioslave to access zip files from KDE.
  34.  *   You can use it in IO_ReadOnly or in IO_WriteOnly mode, and it
  35.  *   behaves just as expected.
  36.  *   It can also be used in IO_ReadWrite mode, in this case one can
  37.  *   append files to an existing zip archive. When you append new files, which
  38.  *   are not yet in the zip, it works as expected, i.e. the files are appended at the end.
  39.  *   When you append a file, which is already in the file, the reference to the
  40.  *   old file is dropped and the new one is added to the zip - but the
  41.  *   old data from the file itself is not deleted, it is still in the
  42.  *   zipfile. so when you want to have a small and garbage-free zipfile,
  43.  *   just read the contents of the appended zip file and write it to a new one
  44.  *   in IO_WriteOnly mode. This is especially important when you don't want
  45.  *   to leak information of how intermediate versions of files in the zip
  46.  *   were looking.
  47.  *   For more information on the zip fileformat go to
  48.  *   http://www.pkware.com/products/enterprise/white_papers/appnote.html
  49.  * @short A class for reading/writing zip archives.
  50.  * @author Holger Schroeder <holger-kde@holgis.net>
  51.  * @since 3.1
  52.  */
  53. class KIO_EXPORT KZip : public KArchive
  54. {
  55. public:
  56.     /**
  57.      * Creates an instance that operates on the given filename.
  58.      * using the compression filter associated to given mimetype.
  59.      *
  60.      * @param filename is a local path (e.g. "/home/holger/myfile.zip")
  61.      */
  62.     KZip( const QString& filename );
  63.  
  64.     /**
  65.      * Creates an instance that operates on the given device.
  66.      * The device can be compressed (KFilterDev) or not (QFile, etc.).
  67.      * @warning Do not assume that giving a QFile here will decompress the file,
  68.      * in case it's compressed!
  69.      * @param dev the device to access
  70.      */
  71.     KZip( QIODevice * dev );
  72.  
  73.     /**
  74.      * If the zip file is still opened, then it will be
  75.      * closed automatically by the destructor.
  76.      */
  77.     virtual ~KZip();
  78.  
  79.     /**
  80.      * The name of the zip file, as passed to the constructor.
  81.      * Null if you used the QIODevice constructor.
  82.      * @return the zip's file name, or null if a QIODevice is used
  83.      */
  84.     QString fileName() { return m_filename; }
  85.  
  86.     /**
  87.      * Describes the contents of the "extra field" for a given file in the Zip archive.
  88.      */
  89.     enum ExtraField { NoExtraField = 0,      ///< No extra field
  90.                       ModificationTime = 1,  ///< Modification time ("extended timestamp" header)
  91.                       DefaultExtraField = 1
  92.     };
  93.  
  94.     /**
  95.      * Call this before writeFile or prepareWriting, to define what the next
  96.      * file to be written should have in its extra field.
  97.      * @param ef the type of "extra field"
  98.      * @see extraField()
  99.      */
  100.     void setExtraField( ExtraField ef );
  101.  
  102.     /**
  103.      * The current type of "extra field" that will be used for new files.
  104.      * @return the current type of "extra field"
  105.      * @see setExtraField()
  106.      */
  107.     ExtraField extraField() const;
  108.  
  109.     /**
  110.      * Describes the compression type for a given file in the Zip archive.
  111.      */
  112.     enum Compression { NoCompression = 0,     ///< Uncompressed.
  113.                DeflateCompression = 1 ///< Deflate compression method.
  114.     };
  115.  
  116.  
  117.     /**
  118.      * Call this before writeFile or prepareWriting, to define whether the next
  119.      * files to be written should be compressed or not.
  120.      * @param c the new compression mode
  121.      * @see compression()
  122.      */
  123.     void setCompression( Compression c );
  124.  
  125.     /**
  126.      * The current compression mode that will be used for new files.
  127.      * @return the current compression mode
  128.      * @see setCompression()
  129.      */
  130.     Compression compression() const;
  131.  
  132.     /**
  133.      * If an archive is opened for writing then you can add a new file
  134.      * using this function.
  135.      * This method takes the whole data at once.
  136.      * @param name can include subdirs e.g. path/to/the/file
  137.      * @param user the user owning the file
  138.      * @param group the group owning the file
  139.      * @param size the size of the file
  140.      * @param data a pointer to the data
  141.      * @return true if successful, false otherwise
  142.      */
  143.     virtual bool writeFile( const QString& name, const QString& user, const QString& group, uint size, const char* data ); // BC: remove reimplementation for KDE-4.0
  144.  
  145.     /**
  146.      * Alternative method for writing: call prepareWriting(), then feed the data
  147.      * in small chunks using writeData(), and call doneWriting() when done.
  148.      * @param name can include subdirs e.g. path/to/the/file
  149.      * @param user the user owning the file
  150.      * @param group the group owning the file
  151.      * @param size unused argument
  152.      * @return true if successful, false otherwise
  153.      */
  154.     virtual bool prepareWriting( const QString& name, const QString& user, const QString& group, uint size );
  155.  
  156.     // TODO(BIC) make virtual. For now it must be implemented by virtual_hook.
  157.     bool writeSymLink(const QString &name, const QString &target,
  158.                         const QString &user, const QString &group,
  159.                         mode_t perm, time_t atime, time_t mtime, time_t ctime);
  160.     // TODO(BIC) make virtual. For now it must be implemented by virtual_hook.
  161.     bool prepareWriting( const QString& name, const QString& user,
  162.                         const QString& group, uint size, mode_t perm,
  163.                         time_t atime, time_t mtime, time_t ctime );
  164.     // TODO(BIC) make virtual. For now it must be implemented by virtual_hook.
  165.     bool writeFile( const QString& name, const QString& user, const QString& group,
  166.                         uint size, mode_t perm, time_t atime, time_t mtime,
  167.                         time_t ctime, const char* data );
  168.     /**
  169.      * Write data to a file that has been created using prepareWriting().
  170.      * @param data a pointer to the data
  171.      * @param size the size of the chunk
  172.      * @return true if successful, false otherwise
  173.      */
  174.     bool writeData( const char* data, uint size ); // TODO make virtual
  175.  
  176.     /**
  177.      * Write data to a file that has been created using prepareWriting().
  178.      * @param size the size of the file
  179.      * @return true if successful, false otherwise
  180.      */
  181.     virtual bool doneWriting( uint size );
  182.  
  183. protected:
  184.     /**
  185.      * Opens the archive for reading.
  186.      * Parses the directory listing of the archive
  187.      * and creates the KArchiveDirectory/KArchiveFile entries.
  188.      * @param mode the mode of the file
  189.      */
  190.     virtual bool openArchive( int mode );
  191.     /// Closes the archive
  192.     virtual bool closeArchive();
  193.  
  194.     /**
  195.      * @internal Not needed for zip
  196.      */
  197.     virtual bool writeDir( const QString& name, const QString& user, const QString& group) { Q_UNUSED(name); Q_UNUSED(user); Q_UNUSED(group); return true; }
  198.     // TODO(BIC) uncomment and make virtual for KDE 4.
  199. //    bool writeDir( const QString& name, const QString& user, const QString& group,
  200. //                        mode_t perm, time_t atime, time_t mtime, time_t ctime );
  201.  
  202. protected:
  203.     virtual void virtual_hook( int id, void* data );
  204.     /** @internal for virtual_hook */
  205.     // from KArchive
  206.     bool writeData_impl( const char* data, uint size );
  207.     bool prepareWriting_impl(const QString& name, const QString& user,
  208.                         const QString& group, uint size, mode_t perm,
  209.                         time_t atime, time_t mtime, time_t ctime);
  210.     bool writeSymLink_impl(const QString &name, const QString &target,
  211.                         const QString &user, const QString &group,
  212.                         mode_t perm, time_t atime, time_t mtime, time_t ctime);
  213. private:
  214.     void abort();
  215.  
  216. private:
  217.     QString m_filename;
  218.     class KZipPrivate;
  219.     KZipPrivate * d;
  220. };
  221.  
  222.  
  223. /**
  224.  * @internal
  225.  */
  226. class KIO_EXPORT KZipFileEntry : public KArchiveFile
  227. {
  228. public:
  229.     /*KZipFileEntry() : st(-1)
  230.       {}*/
  231.     KZipFileEntry( KZip* zip, const QString& name, int access, int date,
  232.                    const QString& user, const QString& group, const QString& symlink,
  233.                    const QString& path, Q_LONG start, Q_LONG uncompressedSize,
  234.                    int encoding, Q_LONG compressedSize) :
  235.         KArchiveFile( zip, name, access, date, user, group, symlink,
  236.                       start, uncompressedSize ),
  237.         m_crc(0),
  238.         m_compressedSize(compressedSize),
  239.         m_headerStart(0),
  240.         m_encoding(encoding),
  241.         m_path( path )
  242.     {}
  243.     int encoding() const { return m_encoding; }
  244.     Q_LONG compressedSize() const { return m_compressedSize; }
  245.  
  246.     /// Only used when writing
  247.     void setCompressedSize(Q_LONG compressedSize) { m_compressedSize = compressedSize; }
  248.  
  249.     /// Header start: only used when writing
  250.     void setHeaderStart(Q_LONG headerstart) { m_headerStart = headerstart; }
  251.     Q_LONG headerStart() const {return m_headerStart; }
  252.  
  253.     /// CRC: only used when writing
  254.     unsigned long crc32() const { return m_crc; }
  255.     void setCRC32(unsigned long crc32) { m_crc=crc32; }
  256.  
  257.     /// Name with complete path - KArchiveFile::name() is the filename only (no path)
  258.     QString path() const { return m_path; }
  259.  
  260.     /**
  261.      * @return the content of this file.
  262.      * Call data() with care (only once per file), this data isn't cached.
  263.      */
  264.     virtual QByteArray data() const;
  265.  
  266.     /**
  267.      * This method returns a QIODevice to read the file contents.
  268.      * This is obviously for reading only.
  269.      * Note that the ownership of the device is being transferred to the caller,
  270.      * who will have to delete it.
  271.      * The returned device auto-opens (in readonly mode), no need to open it.
  272.      */
  273.     QIODevice* device() const; // WARNING, not virtual!
  274.  
  275. private:
  276.     unsigned long m_crc;
  277.     Q_LONG m_compressedSize;
  278.     Q_LONG m_headerStart;
  279.     int m_encoding;
  280.     QString m_path;
  281.     // KDE4: d pointer or at least some int for future extensions
  282. };
  283.  
  284. #endif
  285.